<?php

function oh_is_logged_in()
{
	return $_SESSION['user_login'];
}

function oh_is_admin()
{
	return $_SESSION['user_isadmin'];
}

function oh_require_admin()
{
	if(!oh_is_admin())
	{
		header('Location: login.php');
		die();
	}
}

function check_can_signup_as_GM($user_id)
{	
	$user_id = filter_var($user_id, FILTER_SANITIZE_NUMBER_INT);
	
	$con=oh_sql_connect();
	$sql="SELECT * FROM `oh_users` WHERE `uid`='".$user_id."'";
	
	$result=oh_sql_query($con,$sql);
	
	if(($result->num_rows > 0) && ($row=$result->fetch_assoc()))
	{
		if($row['cleared']!=1)
			return "NOT_CLEARED";
		else
			return "CLEAR";
	}
	return "NOT_FOUND";
}

function check_can_signup_as_player($user_id)
{	
	$user_id = filter_var($user_id, FILTER_SANITIZE_NUMBER_INT);
	
	$con=oh_sql_connect();
	$sql="SELECT * FROM `oh_users` WHERE `uid`='".$user_id."'";
	
	$result=oh_sql_query($con,$sql);
	
	if(($result->num_rows > 0) && ($row=$result->fetch_assoc()))
	{
		if($row['cleared']!=1)
			return "NOT_CLEARED";
		else if($row['slots_gm']<OH_MIN_GMSLOTS_TO_PLAY)
			return "MUST_GM";
		else
			return "CLEAR";
	}
	return "NOT_FOUND";
}

function render_user_list()
{
	$con=oh_sql_connect();
	$sql="SELECT * FROM `oh_users` ORDER BY `lastname`,`firstname`";
	
	$result=oh_sql_query($con,$sql);
	
	if( $result->num_rows > 0)
	{	
		echo "<table>\n";
		echo "<tr>\n";
		echo "<td>Name</td>\n";
		echo "<td>Email</td>\n";
		echo "<td>Cleared?</td>\n";
		echo "<td>GM Slots</td>\n";
		echo "<td>Player Slots</td>\n";
		echo "<td>Operations</td>\n";
		while($row=$result->fetch_assoc())
		{
			echo "<tr>\n";
			echo "<td class=\"scentabledata\">".$row['lastname'].", ".$row['firstname']."</td>\n";
			echo "<td class=\"scentabledata\">".$row['email']."</td>\n";
			if($row['cleared']==1)
				echo "<td class=\"scentabledata\">Yes</td>\n";
			else
				echo "<td class=\"scentabledata\">No</td>\n";
			echo "<td class=\"scentabledata\">".$row['slots_gm']."</td>\n";
			echo "<td class=\"scentabledata\">".$row['slots_player']."</td>\n";
			echo "<td class=\"scentabledata\">";
			if($row['cleared']==1)
				make_button("Un-Clear","");
			else
				make_button("Clear","");
			make_button("View Schedule","schedule.php?user=".$row['uid']);
			make_button("Delete","");
			echo "</td>";
			echo "</tr>\n";
		}		
		echo "</table>\n";
	}
	$con->close();	

}

function email_broadcast($subject,$body)
{
	$con=oh_sql_connect();
	
	$subject=OH_TITLE." - ".$subject;
	
	// select only users who have opted in
	$sql="SELECT * FROM `oh_users` WHERE `recv_email`='1' ORDER BY `lastname`,`firstname`";
	
	$result=oh_sql_query($con,$sql);
	
	if( $result->num_rows > 0)
	{
		while($row=$result->fetch_assoc())
		{
			echo "Emailing ".$row['firstname']." ".$row['lastname']."<br />\n";
			
			$email=$row['email'];
			
			$headers = "From: ". OH_ADMIN_EMAIL ."\r\n".
			"Reply-To: ". OH_ADMIN_EMAIL ."\r\n".
			"X-Mailer: PHP/". phpversion();
						
			mail($email, 
			$subject,
			$body,
			$headers);
		}
	}
}

?>